package com.local.indexconfig.service.impl;

import com.local.common.constants.BusinessException;
import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import com.local.goods.feign.GoodsServiceFeign;
import com.local.goods.pojo.Goods;
import com.local.indexconfig.mapper.IndexConfigMapper;
import com.local.indexconfig.pojo.IndexConfig;
import com.local.indexconfig.service.IndexConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class IndexConfigServiceImpl implements IndexConfigService {

    @Autowired
    private IndexConfigMapper indexConfigMapper;

    @Autowired
    private GoodsServiceFeign goodsServiceFeign;

    @Override
    public PageResultUtil<IndexConfig> findIndexConfigList(Integer configType, Integer page, Integer limit, String sort, String order, Integer isDeleted) {
        Integer start = (page - 1) * limit;
        List<IndexConfig> indexConfigList = indexConfigMapper.findIndexConfigList(configType, start, limit, sort, order, isDeleted);
        Integer totalIndexConfigs = indexConfigMapper.getTotalIndexConfigs(configType, isDeleted);
        PageResultUtil<IndexConfig> indexConfigPageResultUtil = new PageResultUtil<>(indexConfigList, totalIndexConfigs, limit, page);
        return indexConfigPageResultUtil;
    }

    /**
     * 新增
     * @param indexConfig
     * @return
     */
    @Override
    public Integer save(IndexConfig indexConfig) {
        return indexConfigMapper.save(indexConfig);
    }

    /**
     * 修改
     * @param indexConfig
     * @return
     */
    @Override
    public Integer update(IndexConfig indexConfig) {
        return indexConfigMapper.update(indexConfig);
    }

    @Override
    public Integer deleteByIds(Integer[] ids, Integer userId, String dateTime) {
        return indexConfigMapper.deleteByIds(ids, userId, dateTime);
    }


    /**
     * 查询首页各部分推荐商品的列表
     * @param configType
     * @param number
     * @return
     */
    @Override
    public List<IndexConfig> getListByConfigType(Integer configType, Integer number) {
        List<IndexConfig> indexConfigList = indexConfigMapper.findIndexConfigList(configType, 0, number, "config_rank", "asc", 0);
        if (indexConfigList.size() > 0) {
            for (IndexConfig indexConfig : indexConfigList) {
                Integer goodsId = indexConfig.getGoodsId();

                //调用远程服务goods-service，获取goods商品数据
                ResultUtil<Goods> goodsResult = goodsServiceFeign.getSimpleById(goodsId);
                if (goodsResult.getCode() != 200) {
                    throw new BusinessException(goodsResult.getCode(), goodsResult.getMessage());
                }
                Goods goods = goodsResult.getData();
                if (Objects.isNull(goods)) {
                    indexConfig.setGoodsName("");
                    indexConfig.setGoodsCoverImg("");
                    indexConfig.setGoodsIntro("");
                    indexConfig.setSellingPrice(0);
                    indexConfig.setTag("");
                } else {
                    indexConfig.setGoodsName(goods.getGoodsName());
                    indexConfig.setGoodsCoverImg(goods.getGoodsCoverImg());
                    indexConfig.setGoodsIntro(goods.getGoodsIntro());
                    indexConfig.setSellingPrice(goods.getSellingPrice());
                    indexConfig.setTag(goods.getTag());
                }
            }
        }

        return indexConfigList;
    }


}
